local addon, ns = ...
local C = ns.shared.color
local func = ns.shared
local L = ns.L

-- Function to get the current Zone or subzone
local function getZone()
	local subZone = GetSubZoneText() or ""
	local zone = GetRealZoneText() or ""

	if subZone == nil  or subZone == "" then
		return zone
	else
		return subZone
	end
end

-- Function ot return the pvp zone information and the related color to color text.
local function getPVP()
	local p, _, f = GetZonePVPInfo()
	local color = "white"

	if p == "combat" or p == "arena" or p == "hostile" then
		color = "red"
	elseif p == "contested" or p == nil then
		color = "dkyellow"
		p = "contested"
	elseif p == "friendly" then
		color = "dkgreen"
	elseif p == "sanctuary" then
		color = "ltblue"
	end

	return p, color
end

local zonetext_onevent = function (self)
	local dataobj = self.obj or LibStub:GetLibrary("LibDataBroker-1.1"):GetDataObjectByName(L["ZoneText"])

	local zone = getZone()
	local _, color = getPVP()
	
	dataobj.text = C[color](zone)
end

-- Function to Populate the ZoneText Tooltip
local function drawZoneTextTip()
	local pvp, color = getPVP()
    pvp = gsub(pvp,"^%l", string.upper)

	tooltip:AddHeader(C.dkyellow(L["ZoneText"]))
	tooltip:AddLine(" ")

	tooltip:AddLine(C.ltblue(L["Location Information"]))
	tooltip:AddSeparator()
	tooltip:AddLine(C.ltyellow(L["Zone"] .. ":"), GetRealZoneText())
	tooltip:AddLine(C.ltyellow(L["Sub-Zone"] .. ":"), GetSubZoneText())
	tooltip:AddLine(C.ltyellow(L["PVP Flag"] .. ":"), C[color](pvp))
	tooltip:AddLine(" ")

	tooltip:AddLine(C.ltblue(L["Home"]))
	tooltip:AddSeparator()
	tooltip:AddLine(C.ltyellow(L["Inn"]), GetBindLocation())
	tooltip:AddLine(" ")
	
	local line, column = tooltip:AddLine()
	tooltip:SetCell(line, 1, C.dkgreen(L["Hint: Click to open the World Map."]), nil, nil, 2)	
end


local zonetext_events = {
	"PLAYER_LOGIN",
	"ZONE_CHANGED",
	"ZONE_CHANGED_INDOORS",
	"ZONE_CHANGED_NEW_AREA",
	"PLAYER_ENTERING_WORLD",
}


local zonetext_onclick = function()
	if GUILockDown == nil then 
		ToggleFrame(WorldMapFrame)
	elseif GUILockDown == 1 then 
		return 
	end
end

local zonetext_init = function(obj)
	if not obj then return end

	obj = obj.obj or LibStub:GetLibrary("LibDataBroker-1.1"):GetDataObjectByName(L["ZoneText"])

	obj.OnLeave = function ()
		if tooltip then
			LibQTip:Release(tooltip)
			tooltip = nil
		end
	end

	obj.OnEnter = function(self)
		tooltip = LibQTip:Acquire("GPSTT", 2, LEFT, RIGHT) 
		tooltip:SetScale(1)

		drawZoneTextTip()

		tooltip:SmartAnchorTo(self)
		--tooltip:UpdateScrolling()
		tooltip:Show()
	end
end

--[[
local zonetext_tooltip = function(t)

	t:AddLine(L["ZoneText"])
	t:AddLine(" ")
	t:AddDoubleLine(C.white(L["Zone"]), C.white(GetRealZoneText()))
	t:AddDoubleLine(C.white(L["Sub-Zone"]), C.white(GetSubZoneText()))
	t:AddLine(" ")
	t:AddLine(C.dkgreen(L["Hint: Click to open the World Map."]))
end
]]

Broker_Everything:Register(L["Zonetext"],
	GetItemIcon(11105),
	zonetext_init,
	zonetext_onevent,
	zonetext_events,
	zonetext_onclick,
	0, --zonetext_tooltip,
	nil,
	nil)
